package com.yuanluludev.lib_base.ext

import android.text.Editable
import android.view.View
import android.widget.EditText
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.actor
import kotlinx.coroutines.delay


/**
 *   判空
 *   ifNotNull(per1,per2){  }
 */
fun ifNotNull(vararg obj: Any?, block: () -> Unit) {
    if (obj.filterNotNull().isNotEmpty()) {
        block()
    }
}

/**
 * 防止重复点击
 */
@DelicateCoroutinesApi
fun View.singleClick(time: Long = 1500, block: suspend () -> Unit) {
    val action = GlobalScope.actor<Unit>(context = Dispatchers.Main) {
        for (event in channel) {
            block()
            delay(time)
        }
    }
    setOnClickListener {
        action.trySend(Unit).isSuccess
    }
}

/**
 * editText设置文字
 */
fun EditText.setTextString(source: String = "") {
    text = Editable.Factory.getInstance().newEditable(source)
}